#ifndef LOGIN_H
#define LOGIN_H

#include <QWidget>
#include <QEvent>
#include <QMouseEvent>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QPainter>

class my_register;
namespace Ui {
class login;
}

class login : public QWidget
{
    Q_OBJECT

public:
    explicit login(QWidget *parent = nullptr);
    ~login();
    void paintEvent(QPaintEvent* event);

    void login_successed(const QString& username,const QString& nickname,const QString& path,const QString& last_login_time);
    static login* getInstance();
    static void destroyInstance();

signals:
    void loginSuccess(const QString& username,const QString& nickname,const QString& path,const QString& last_login_time);
    void reg();
private slots:
    void on_radioButton_clicked();

    void on_login_pushButton_clicked();

    void on_close_toolButton_clicked();

    void on_register_pushButton_clicked();

private:
    Ui::login *ui;
    static login* instance;
};

#endif // LOGIN_H

class DragWidgetFilter:public QObject{
public:
    DragWidgetFilter(QObject* parent): QObject(parent){}

    bool eventFilter(QObject *object,QEvent *event)
    {
        auto w=dynamic_cast<QWidget*>(object);
        if(!w)
        {
            return false;
        }
        if(event->type()==QEvent::MouseButtonPress)
        {
            auto ev=dynamic_cast<QMouseEvent*>(event);
            if(!ev) return false;
            pos=ev->pos();
        }
        else if(event->type()==QEvent::MouseMove)
        {
            auto ev=dynamic_cast<QMouseEvent*>(event);
            if(!ev) return false;
            if(ev->buttons()&Qt::MouseButton::LeftButton)
            {
                w->move(ev->globalPosition().toPoint()-pos);
            }
        }
        return QObject::eventFilter(object,event);
    }
private:
    QPoint pos;
};
